<template>
	<view class="page-wallet">
		<view class="uni-padding-wrap uni-common-mt">
			<uni-segmented-control :current="current" :values="items" :style-type="styleType" active-color="#31E8D6"
				@clickItem="onClickItem" />
		</view>
		<view class="form">
			<view class="item">
				<view class="label">
					钱包名称
				</view>
				<view class="input-box">
					<input type="text" placeholder-style="color:#666" placeholder="请输入钱包名称" v-model="walletName" />
				</view>
				<view class="tip">名称可由汉字、字母或数字组成，限制4-12个字符</view>
			</view>
			<view class="item">
				<view class="label">
					安全密码
				</view>
				<view class="input-box">
					<input type="text" password="" placeholder-style="color:#666" placeholder="请输入安全密码"
						v-model="password" />
				</view>
				<view class="tip">密码必须为8-16位大小写字母和数字的组合</view>
			</view>
			<view class="item" v-if="current == 0">
				<view class="label">
					请填写您的助记词，英文单词请按空格分隔单词
				</view>
				<view class="input-box">
					<textarea v-model="mnemonic" placeholder="由12个英文单词组成,单词之间请用空格分隔,首尾勿加空格!" placeholder-style="color:#666"/>
				</view>
			</view>
			<view class="item" v-if="current == 1">
				<view class="label">
					请输入你的私钥
				</view>
				<view class="input-box">
					<textarea v-model="privateKey" placeholder="64位数字字母组成" placeholder-style="color:#666" />
				</view>
			</view>
		</view>
		<view class="button-box-padding">
			<button class="btn-primary" @click="submit()">确认</button>
		</view>
	</view>
</template>

<script>
	import api from "@/utils/api/wallet/index.js"
	import util from '@/common/util.js'

	export default {
		data() {
			return {
				styleType: "text",
				items: [
					'助记词',
					'私钥',
				],
				current: 0,
				privateKey: "",
				mnemonic: "",
				walletName: "",
				password: "",
			}
		},
		mounted() {
			console.log(this.current)
		},
		methods: {
			onClickItem(val) {
				//console.log(JSON.stringify(index))
				if (this.current !== val.currentIndex) {
					this.current = val.currentIndex
				}
			},
			submit() {
				if (this.current == 1) {
					this.importPrivateKey()
				} else {
					this.importMnemonic()
				}
			},
			async importPrivateKey() {
				let checkWalletName = util.checkItem("wallet_name", this.walletName)
				let checkPassword = util.checkItem("password", this.password)
				let checkPrivateKey = util.checkItem("private_key", this.privateKey)
				if (!checkWalletName.status) {
					util.showError(checkWalletName.message);
					return
				}
				if (!checkPassword.status) {
					util.showError(checkPassword.message);
					return
				}
				if (!checkPrivateKey.status) {
					util.showError(checkPrivateKey.message);
					return
				}

				let post = {
					'wallet_name': this.walletName,
					'password': this.password,
					'private_key': this.privateKey
				}

				let result = await api.importPrivateKey(post)
				if (result.status == 1) {
					uni.setStorageSync("user", result.data);
					uni.reLaunch({
						url: "../home"
					})
				}
			},
			async importMnemonic() {
				let checkWalletName = util.checkItem("wallet_name", this.walletName)
				let checkPassword = util.checkItem("password", this.password)
				let checkMnemonic = util.checkItem("mnemonic", this.mnemonic)
				//return;
				if (!checkWalletName.status) {
					util.showError(checkWalletName.message);
					return
				}
				if (!checkPassword.status) {
					util.showError(checkPassword.message);
					return
				}
				//console.log()
				if (!checkMnemonic.status) {
					util.showError(checkMnemonic.message);
					return
				}

				let post = {
					'wallet_name': this.walletName,
					'password': this.password,
					'mnemonic': this.mnemonic
				}

				let result = await api.importMnemonic(post)
				if (result.status == 1) {
					uni.setStorageSync("user", result.data);
					uni.reLaunch({
						url: "../home"
					})
				}
			}
		}
	}
</script>

<style>

</style>
